package com.thisblog.homepage.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thisblog.common.core.enums.QueryModeEnum;
import com.thisblog.common.core.result.Result;
import com.thisblog.homepage.pojo.entity.SysMenu;
import com.thisblog.homepage.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "菜单接口")
@RestController
@RequestMapping("/api.admin/v1/menus")
@Slf4j
@AllArgsConstructor
public class MenuController {

    private ISysMenuService iSysMenuService;

    @ApiOperation(value = "菜单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum")
    })
    @GetMapping
    public Result list(String queryMode, String name, Long roleId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode);
        LambdaQueryWrapper<SysMenu> baseQuery = new LambdaQueryWrapper<SysMenu>()
                .orderByAsc(SysMenu::getSort)
                .orderByDesc(SysMenu::getGmtModified)
                .orderByDesc(SysMenu::getGmtCreate);
        List list;
        switch (queryModeEnum) {
            case LIST:
                baseQuery = baseQuery.like(StrUtil.isNotBlank(name), SysMenu::getName, name);
                list = iSysMenuService.listMenuVO(baseQuery);
                break;
            case TREE:
                list = iSysMenuService.listTreeVO(baseQuery);
                break;
            case ROUTER:
                list = iSysMenuService.listRouterVO();
                break;
            default:
                list = iSysMenuService.list(baseQuery);
                break;
        }
        return Result.success(list);
    }
}
